package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    /**
     * 接口描述：套餐分页查询
     * @param setmealPageQueryDTO
     * @return com.sky.result.PageResult
     * 参数类型：简单参数
     * 请求方法：GET
     * 请求地址：/admin/setmeal/page
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 接口描述：新增套餐
     * @param setmealDTO
     * @return com.sky.result.Result
     * 参数类型：请求体参数
     * 请求方法：POST
     * 请求地址：/admin/setmeal
     */
    @PostMapping
    @CachePut(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }
    /**
     * 接口描述：修改套餐
     * @param setmealDTO
     * @return Result
     * 参数类型：请求体参数
     * 请求方法：PUT
     * 请求地址：/admin/setmeal
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.update(setmealDTO);
        return Result.success();
    }
    /**
     * 接口描述:根据id查询套餐
     * @param id
     * @return setmealVO
     * 参数类型：路径参数
     * 请求方法：GET
     * 请求地址：/admin/setmeal/{id}
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }
    /**
     * 接口描述:批量删除套餐
     * @param ids
     * @return Result
     * 参数类型：简单参数
     * 请求方法：DELETE
     * 请求地址：/admin/setmeal
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        setmealService.delete(ids);
        return Result.success();
    }
    /**
     * 接口描述:套餐起售停售
     * @param status
     * @return Result
     * 参数类型：路径参数
     * 请求方法：POST
     * 请求地址：/admin/setmeal/status/{status}
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status, id);
        return Result.success();
    }
}
